package com.gis.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gis.dto.UserDTO;
import com.gis.entity.LayerData;
import com.gis.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gis.entity.User;
import com.gis.result.ResponseResult;
import com.gis.service.UserService;

import lombok.RequiredArgsConstructor;
import com.gis.annotation.RequirePermission;


@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Tag(name = "[产品]用户", description = "用户管理接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     */
//    @Operation(summary = "用户注册", description = "创建新用户")
//    @PostMapping("/register")
//    public ResponseResult<Map<String, Object>> register(
//            @RequestParam("usertype") String usertype,
//            @RequestParam("username") String username,
//            @RequestParam("password") String password) {
//        Map<String, Object> result = new HashMap<>();
//        try {
//            result = userService.register(username, password, usertype);
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.put("msg", "注册失败");
//            result.put("code", 500);
//            result.put("data", e.getMessage());
//            return ResponseResult.fail(result);
//        }
//        return ResponseResult.success(result);
//    }
    @Operation(summary = "用户注册", description = "创建新用户")
    @PostMapping("/register")
    public ResponseResult<Map<String, Object>> register(@RequestBody UserDTO userDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 从 userDTO 中获取 JSON 数据
            result = userService.register(userDTO.getUsername(), userDTO.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
//            result.put("msg", "注册失败");
//            result.put("code", 500);
            result.put("message", e.getMessage());
            return ResponseResult.fail(result);
        }
        return ResponseResult.success(result);
    }

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "用户身份验证")
    @PostMapping("/login")
    public ResponseResult<Map<String, Object>> login(@RequestBody UserDTO userDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = userService.login(userDTO.getUsername(), userDTO.getPassword());

            if (result.containsKey("code") && (Integer) result.get("code") == 200) {
                // 获取用户类型
                User user = userService.getUserByUsername(userDTO.getUsername());
                String userType = user != null ? user.getUsertype() : "user";

                // 登录成功，生成包含用户类型的 token
                String token = JwtUtil.generateToken(userDTO.getUsername(), userType);
                result.put("token", token); // 将 token 放入响应数据中
                result.put("usertype", userType); // 同时返回用户类型
            }

        } catch (Exception e) {
            e.printStackTrace();
//            result.put("msg", "登录失败");
//            result.put("code", 500);
            result.put("message", e.getMessage());
            return ResponseResult.fail(result);
        }
        return ResponseResult.success(result);
    }

    /**
     * 修改密码
     */
//    @Operation(summary = "修改密码", description = "更新用户密码")
//    @PostMapping("/update_password")
//    public ResponseResult<Map<String, Object>> updatePassword(
//            @RequestParam("username") String username,
//            @RequestParam("newPassword") String newPassword) {
//        Map<String, Object> result = new HashMap<>();
//        try {
//            result = userService.updatePassword(username, newPassword);
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.put("msg", "修改密码失败");
//            result.put("code", 500);
//            result.put("data", e.getMessage());
//            return ResponseResult.fail(result);
//        }
//        return ResponseResult.success(result);
//    }
    @Operation(summary = "修改密码", description = "更新用户密码")
    @PostMapping("/update_password")
    public ResponseResult<Map<String, Object>> updatePassword(@RequestBody UserDTO userDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 使用用户名和新密码来更新
            result = userService.updatePassword(userDTO.getUsername(), userDTO.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
//            result.put("msg", "修改密码失败");
//            result.put("code", 500);
            result.put("message", e.getMessage());
            return ResponseResult.fail(result);
        }
        return ResponseResult.success(result);
    }

    /**
     * 删除用户 - 需要管理员权限
     */
    @Operation(summary = "删除用户", description = "删除指定用户名的用户")
    @DeleteMapping("/delete")
    @RequirePermission("admin") // 添加管理员权限注解
    public ResponseResult<Map<String, Object>> deleteUser(@RequestBody UserDTO userDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = userService.deleteUser(userDTO.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
//            result.put("msg", "删除用户失败");
//            result.put("code", 500);
            result.put("message", e.getMessage());
            return ResponseResult.fail(result);
        }
        return ResponseResult.success(result);
    }

    /**
     * 查询用户列表
     */
    @Operation(summary = "获取用户列表", description = "获取用户列表")
    @GetMapping("/listUser")
    @RequirePermission("admin") // 添加管理员权限注解
    public ResponseResult<List<User>> getUserList() {
        return ResponseResult.success("查询成功", userService.list());
    }

    /**
     * 修改用户权限 - 需要管理员权限
     */
    @Operation(summary = "修改用户权限", description = "修改用户权限")
    @PostMapping("/updateUsertype")
    @RequirePermission("admin") // 添加管理员权限注解
    public ResponseResult<Map<String, Object>> updateUsertype(@RequestBody UserDTO userDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = userService.updateUsertype(userDTO.getUsername(), userDTO.getUsertype());
        } catch (Exception e) {
            e.printStackTrace();
//            result.put("msg", "修改权限失败");
//            result.put("code", 500);
            result.put("message", e.getMessage());
            return ResponseResult.fail(result);
        }
        return ResponseResult.success(result);
    }

    @Operation(summary = "新增用户", description = "管理员新增用户")
    @PostMapping("/addUser")
    @RequirePermission("admin") // 添加管理员权限注
    public ResponseResult<Map<String, Object>> addUser(@RequestBody UserDTO userDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 从 userDTO 中获取 JSON 数据
            result = userService.addUser(userDTO.getUsername(), userDTO.getPassword(), userDTO.getUsertype());
        } catch (Exception e) {
            e.printStackTrace();
//            result.put("msg", "新增用户失败");
//            result.put("code", 500);
            result.put("message", e.getMessage());
            return ResponseResult.fail(result);
        }
        return ResponseResult.success(result);
    }

}
